package ch14;

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Fan extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        BorderPane mainpane = new BorderPane();
        FanPane fan1 = new FanPane();
        mainpane.setCenter(fan1);
        Controllers buttons = new Controllers();
        mainpane.setBottom(buttons);
        Scene scene = new Scene(mainpane);
        primaryStage.setScene(scene);
        primaryStage.show();
        Timeline animation1 = new Timeline(new KeyFrame(Duration.millis(100), e -> fan1.rotate()));
        animation1.setCycleCount(Timeline.INDEFINITE);
        animation1.play();
        buttons.buttonPause.setOnAction(e -> animation1.pause());
        buttons.buttonStart.setOnAction(e -> animation1.play());
        buttons.buttonReverse.setOnAction(e -> fan1.rotateReverse());
    }

    class FanPane extends Pane {
        double rotateAngle = 18.0;
        double rotateBase = 0.0;

        FanPane() {
            setWidth(100);
            setHeight(100);
            print();
        }

        void rotate() {
            setRotate(rotateBase);
            rotateBase += rotateAngle;
        }

        void rotateReverse() {
            rotateAngle *= -1;
        }

        void print() {
            getChildren().clear();
            Circle c = new Circle(50);
            DoubleBinding r = new DoubleBinding() {
                {
                    super.bind(widthProperty(), heightProperty());
                }

                @Override
                protected double computeValue() {
                    return Math.min(getWidth(), getHeight());
                }
            };
            c.radiusProperty().bind(r.divide(2));
            c.centerXProperty().bind(widthProperty().divide(2));
            c.centerYProperty().bind(heightProperty().divide(2));
            c.setStroke(Color.BLACK);
            c.setFill(Color.WHITE);
            Arc arc1 = new Arc(0, 0, 40, 40, 30, 30);
            arc1.centerXProperty().bind(widthProperty().divide(2));
            arc1.centerYProperty().bind(heightProperty().divide(2));
            arc1.radiusXProperty().bind(r.divide(2.5));
            arc1.radiusYProperty().bind(r.divide(2.5));
            arc1.setType(ArcType.ROUND);
            Arc arc2 = new Arc(0, 0, 40, 40, 120, 30);
            arc2.centerXProperty().bind(widthProperty().divide(2));
            arc2.centerYProperty().bind(heightProperty().divide(2));
            arc2.radiusXProperty().bind(r.divide(2.5));
            arc2.radiusYProperty().bind(r.divide(2.5));
            arc2.setType(ArcType.ROUND);
            Arc arc3 = new Arc(0, 0, 40, 40, 210, 30);
            arc3.centerXProperty().bind(widthProperty().divide(2));
            arc3.centerYProperty().bind(heightProperty().divide(2));
            arc3.radiusXProperty().bind(r.divide(2.5));
            arc3.radiusYProperty().bind(r.divide(2.5));
            arc3.setType(ArcType.ROUND);
            Arc arc4 = new Arc(0, 0, 40, 40, 300, 30);
            arc4.centerXProperty().bind(widthProperty().divide(2));
            arc4.centerYProperty().bind(heightProperty().divide(2));
            arc4.radiusXProperty().bind(r.divide(2.5));
            arc4.radiusYProperty().bind(r.divide(2.5));
            arc4.setType(ArcType.ROUND);
            getChildren().addAll(c, arc1, arc2, arc3, arc4);
        }
    }

    class Controllers extends HBox {
        Button buttonPause = new Button("暂停");
        Button buttonStart = new Button("开始");
        Button buttonReverse = new Button("反转");

        Controllers() {
            getChildren().addAll(buttonPause, buttonStart, buttonReverse);
            setAlignment(Pos.CENTER);
        }
    }
}